#include "netmain.h"
#include <QTcpSocket>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QDebug>
#define CARID 0

NetMain *NetMain::obj = nullptr;

NetMain::NetMain(QObject *parent)
    : QObject{parent}
    , client(new QTcpSocket)
{
    connect(client, &QTcpSocket::readyRead,
            this, &NetMain::clientReadyReadSlot);
    connectToHost();
}

NetMain *NetMain::getObject()
{
    if(obj == nullptr)
        obj = new NetMain;
    return obj;
}

void NetMain::carPosChanged(int roundId, int roundPos)
{
    QJsonObject obj;
    obj.insert("type", 3001);
    obj.insert("car_id", CARID);
    obj.insert("round_id", roundId);
    obj.insert("round-pos", roundPos);
    client->write(QJsonDocument(obj).toJson());
}

void NetMain::clientReadyReadSlot()
{
    QByteArray data = client->readAll();
    int count = 0;  //当前括号数量
    int start = -1; //开始的索引
    for(int i = 0; i < data.length(); i++)
    {
        if(data.at(i) == '{')
        {
            count++;
            if(start == -1)
            {
                start = i;
            }
        }
        else if(data.at(i) == '}')
        {
            count--;
            if(count == 0)
            {
                QByteArray temp = data.mid(start, i-start+1);
                start = -1;
                QJsonObject obj = QJsonDocument::fromJson(temp).object();
                handleFrame(obj);
            }
        }
    }
}

void NetMain::connectToHost()
{
    if(client->state() == QTcpSocket::ConnectedState)
        return;
    client->connectToHost("127.0.0.1", 10010);
    bool ok =  client->waitForConnected(1000);
    if(ok)
        sendIdentity();
}

void NetMain::sendIdentity()
{
    QJsonObject obj;
    obj.insert("type", 5000);
    obj.insert("identity",2);
    client->write(QJsonDocument(obj).toJson());
}

void NetMain::handleFrame(const QJsonObject &obj)
{
    int type = obj.value("type").toInt();
    if(type == 4001)
        handleCarStatusFrame(obj);
    else if (type ==4002) {
        handleCarLightFrame(obj);
    }
    else if (type ==4003) {
        handleCarAlarmFrame(obj);
    }
    else if (type == 4004) {
        handleCarAlarmLightFrame(obj);
    }
}

void NetMain::handleCarAlarmFrame(const QJsonObject &obj)
{
    int carId = obj.value("car_id").toInt();
    if(carId != CARID)
        return;
    bool sw = obj.value("sw").toBool();
    emit setCarAlarmSig(sw);
}

void NetMain::handleCarAlarmLightFrame(const QJsonObject &obj)
{
    int carId = obj.value("car_id").toInt();
    if(carId != CARID)
        return;
    bool sw = obj.value("sw").toBool();
    emit setCarAlarmLightSig(sw);
}

void NetMain::handleCarLightFrame(const QJsonObject &obj)
{
    int carId = obj.value("car_id").toInt();
    if(carId != CARID)
        return;
    bool sw = obj.value("sw").toBool();
    emit setCarLightSig(sw);
}

void NetMain::handleCarStatusFrame(const QJsonObject &obj)
{
    int carId = obj.value("car_id").toInt();
    if(carId != CARID)
        return;
    bool sw = obj.value("sw").toBool();
    emit setCarStatusSig(sw);
}






